package com.optiktv.web.jpa.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;

import com.optiktv.web.jpa.bean.Monthly;
import com.optiktv.web.jpa.bean.Order;
import com.optiktv.web.jpa.dao.OrderDao;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Repository
//@Transactional(readOnly = true)
public class OrderDaoImpl implements OrderDao {

    private EntityManager em = null;

    /**
     * Sets the entity manager.
     */
    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    /**
     * Find Orders.
     */
    public Order findOrderByOrdernum(Long id) {
        return em.find(Order.class, id);
    }

    /**
     * Find Orders using a start index and max number of results.
     */
    @SuppressWarnings("unchecked")
    public Collection<Order> findOrders(final int startIndex, final int maxResults) {
        return em.createQuery("select p from Order p order by p.orderNum")
            .setFirstResult(startIndex).setMaxResults(maxResults).getResultList();
    }

    /**
     * Find Orders.
     */
    @SuppressWarnings("unchecked")
    public Collection<Order> findOrders() {
        return em.createQuery("select p from Order p").getResultList();
    }

    /**
     * Find Orders by month
     */
    @SuppressWarnings("unchecked")
    public Collection<Order> findOrdersByMonth(int year, int month) {
    	
    	Calendar cal = Calendar.getInstance();
    	cal.clear();
    	cal.set(year, month, 1);
    	Date startDate = cal.getTime();
    	cal.add(Calendar.MONTH, 1);
    	Date endDate = cal.getTime();
    	
        return em.createQuery("select o from Order o where o.orderdate >= :startDate and o.orderdate < :endDate  order by o.ordernum").
        setParameter("startDate", startDate.getTime()). 
        setParameter("endDate", endDate.getTime()).getResultList();
    }

    /**
     * Saves Order.
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public Order save(Order Order) {
        return em.merge(Order);
    }

    /**
     * Deletes Order.
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void delete(Order Order) {
        em.remove(em.merge(Order));
    }

    @SuppressWarnings("unchecked")
	private void getMonthlyReport(int year) {
		/**
		 * 1. Sales to Provisioning
		 * 2. Prov to Install
		 * 3. Installation
		 * 4. Sum of above 3 => Overall
		 * 5. Customer Satisfaction
		 */
		
		Monthly monthly = new Monthly();
		
		//1
		java.util.List<Float> sales = new ArrayList<Float>();
		for (int i = 0; i < 12; i++)
		{
			sales.add(i + 0.5f);
		}
		//Calculate using Hibernate
		monthly.setSales(sales);
		
		
		//2
		List<Float> provs = new ArrayList<Float>();
		//Calculate using Hibernate
		monthly.setProvs(provs);

		//3
		List<Float> installs = new ArrayList<Float>();
		//Calculate using Hibernate
		monthly.setInstalls(installs);

		
		//4
		Collection<Float> overall = new ArrayList<Float>();
		for (int i = 0; i < sales.size(); i++)
		{
			overall.add(sales.get(i) + provs.get(i) + installs.get(i));
		}
		
		
		//5
		Collection<Float> csats = new ArrayList<Float>();
		//Calculate using Hibernate
		monthly.setCsats(csats);
		
		return;
	}


}
